/*
Copyright 2015 shizhefei（LuckyJayce）

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package com.qdxht.dispenser.ui.models;

import com.qdxht.dispenser.base.BaseActivity;
import com.qdxht.dispenser.http.api.WithdrawalListApi;
import com.qdxht.dispenser.http.model.HttpListData;
import com.qdxht.dispenser.ui.models.entity.WithdrawalListEntity;
import com.hjq.http.EasyHttp;
import com.hjq.http.model.ResponseClass;
import com.shizhefei.mvc.IDataSource;
import java.util.List;

public class WithdrawalListDataSource implements IDataSource<List<WithdrawalListEntity>> {

	BaseActivity activity;
	private int pageIndex = 1;
	boolean hasMore;

	public WithdrawalListDataSource(BaseActivity activity){
		this.activity = activity;
	}

	@Override
	public List<WithdrawalListEntity> refresh() throws Exception {
		return loadBooks(1);
	}

	@Override
	public List<WithdrawalListEntity> loadMore() throws Exception {
		return loadBooks(pageIndex + 1);
	}

	private List<WithdrawalListEntity> loadBooks(int pageIndex) throws Exception {
		HttpListData<WithdrawalListEntity> result = EasyHttp.post(activity)
				.api(new WithdrawalListApi(pageIndex))
				.execute(new ResponseClass<HttpListData<WithdrawalListEntity>>() {});
		this.pageIndex = pageIndex;
		hasMore = result.getData().isLastPage();
		return result.getData().getRecord();
	}

	@Override
	public boolean hasMore() {
		return hasMore;
	}
}
